package com.share.bigdata.hive._main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.share.common.utils.DbUtil;
import com.share.common.utils.FileUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

//hive与phoenix应用冲突不能放在一起,放在一起无法启动,需要分开
public class MainHive {


    public static void main(String[] args) {
        try {
            //读取配置
            String KEY_PRE = "spring.datasource.dynamic.datasource.hive.";
            Properties properties = FileUtil.readPropertiesFromCp("application-dev.properties");
            String driver = properties.getProperty(KEY_PRE + "driver-class-name");
            String url = properties.getProperty(KEY_PRE + "url");
            String username = properties.getProperty(KEY_PRE + "username");
            String password = properties.getProperty(KEY_PRE + "password");

            // 加载Phoenix JDBC驱动
            Class.forName(driver);

            // 建立连接
            Connection conn = DriverManager.getConnection(url,username,password);

            // 创建Statement
            Statement statement = conn.createStatement();

            // 执行SQL查询
            ResultSet rs = statement.executeQuery("select * from test_hive limit 10"); // 替换为你的表名

            // 处理结果
            JSONArray convert = DbUtil.convert(rs);

            System.out.println(JSON.toJSON(convert)); // 替换为你的列名

            // 关闭资源
            rs.close();
            statement.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
